﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;

namespace TA.Turbo.Controls
{
	/// <summary>
	/// Drop down list control which also supports editing and multi-level grouping.
	/// </summary>
	public class ComboBox : BaseComplexListControl
	{
		#region PUBLIC
		#region INIT
		/// <summary>
		/// Ctor.
		/// </summary>
		public ComboBox()
		{
		}
		#endregion
		#region PROPS
		#region DEF
		/// <summary>
		/// Props.
		/// </summary>
		protected static new Properties _clientProperties = Define(BaseComplexListControl._clientProperties, new[] {
			new PropertyDefinition<bool>("Editable", "Ed", true),
		});
		/// <summary>
		/// Props.
		/// </summary>
		public override Properties ClientProperties
		{
			get
			{
				return _clientProperties;
			}
		}
		#endregion
		/// <summary>
		/// Gets or sets a flag; if true, the control allows typing in a custom value (one not from the drop down list), otherwise false.
		/// </summary>
		public bool Editable
		{
			get
			{
				return this.GetProperty<bool>("Editable");
			}
			set
			{
				this.ViewState["Editable"] = value;
			}
		}
		#endregion
		#endregion
		#region INTERNAL
		#region METHODS
		#region INIT
		/// <summary>
		/// Registers ComboBox.js.
		/// </summary>
		protected internal override void RegisterIncludes(IncludeCollection includes)
		{
			base.RegisterIncludes(includes);

			includes.Add(Script.Element_PerformRecursive);
			includes.Add(Script.Element_AddClass);
			includes.Add(Script.Element_HasClass);
			includes.Add(Script.Element_RemoveClass);
			includes.Add(Script.CSS);
			includes.Add(Script.KeyPressHandler);
			includes.Add(new StyleResourceInclude("TA.Turbo.Res.CSS.ComboBox.css", typeof(ComboBox)));
			if (this.Page.Request.Browser.Browser.Contains("IE") && this.Page.Request.Browser.MajorVersion <= 7)
			{
				includes.Add(new StyleResourceInclude("TA.Turbo.Res.CSS.ComboBox_IE67.css", typeof(ComboBox)));
			}
			includes.Add(new ScriptResourceInclude("TA.Turbo.Res.JS.ListControl.ComboBox.js", typeof(ComboBox)));
		}
		#endregion
		#region RENDER
		/// <summary>
		/// Adds the "position" style (for dropdown functionality).
		/// </summary>
		/// <param name="writer"></param>
		protected override void AddAttributesToRender(HtmlTextWriter writer)
		{
			this.CssClass = (this.CssClass ?? "").Replace(" combobox", "") + " combobox";
			base.AddAttributesToRender(writer);
		}
		/// <summary>
		/// Renders the contents of this control: the text box, the drop down button and the drop down list.
		/// </summary>
		/// <param name="writer"></param>
		protected override void RenderContents(HtmlTextWriter writer)
		{
			writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
			writer.RenderBeginTag(HtmlTextWriterTag.Input);
			writer.RenderEndTag();

			writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundImage,
				string.Format("url({0})",
					Page.ClientScript.GetWebResourceUrl(typeof(ComboBox),
						"TA.Turbo.Res.Img.ComboBoxDownButton.png")));
			writer.AddAttribute(HtmlTextWriterAttribute.Class, "button");
			writer.RenderBeginTag(HtmlTextWriterTag.Div);
			writer.RenderEndTag();

			writer.AddAttribute(HtmlTextWriterAttribute.Class, "list");
			writer.RenderBeginTag(HtmlTextWriterTag.Div);

			base.RenderContents(writer);

			writer.RenderEndTag();
		}
		/// <summary>
		/// Renders the beginning tag of an item group.
		/// </summary>
		/// <param name="writer"></param>
		/// <param name="item"></param>
		protected override void RenderItemGroupBeginTag(System.Web.UI.HtmlTextWriter writer, ListItem item)
		{
			writer.AddAttribute(HtmlTextWriterAttribute.Class, "group");
			writer.RenderBeginTag(HtmlTextWriterTag.Div);
		}
		/// <summary>
		/// Renders an option item.
		/// </summary>
		/// <param name="writer"></param>
		/// <param name="item"></param>
		public override void RenderItemOption(HtmlTextWriter writer, ListItem item)
		{
			item.CssClass = (item.CssClass ?? "").Replace(" option", "") + " option";

			writer.AddAttribute(HtmlTextWriterAttribute.Value, item.Value);
			writer.AddAttribute("text", item.Text);
			item.AddAttributes(writer);
			writer.RenderBeginTag(HtmlTextWriterTag.Div);

			writer.Write(item.Text);

			writer.RenderEndTag();
		}
		#endregion
		#endregion
		#endregion
	}
}
